﻿using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace Barkod.Utility
{
    public class FileManager
    {
        public static void Write(string fileName, byte[] content)
        {
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        bw.Write(content);
                        bw.Flush();
                    }
                }
            }
            catch (System.Exception Ex)
            {
                throw Ex;
            }
        }

        public static byte[] Read(string fileName)
        {
            FileInfo fi = new System.IO.FileInfo(fileName);
            byte[] content = new byte[fi.Length];

            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

            fs.Read(content, 0, (int)fi.Length);
            fs.Close();

            return content;
        }

        public static string ReadStream(string fileName)
        {
            string content = "";
            if (File.Exists(fileName))
                using (StreamReader sr = new StreamReader(fileName))
                {
                    content = sr.ReadToEnd();
                }
            return content;
        }

        public static void WriteStream(string fileName, string content)
        {
            using (StreamWriter sw = new StreamWriter(fileName))
            {
                sw.Write(content.ToCharArray());
            }
        }

        public static void RunFile(string fileName, byte[] content)
        {
            FileManager.Write(fileName, content);

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = fileName;
            info.UseShellExecute = true;
            info.ErrorDialog = true;
            Process process = Process.Start(info);
        }

        public static void RunFile(string fileName)
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = fileName;
            process.StartInfo.Verb = "Open";
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            process.Start();
        }
        public static string SaveFile(string title, string filter, params string[] FN)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            string name = FN.Length > 0 ? FN[0] : Application.ProductName;
            int n = name.LastIndexOf(".") + 1;
            if (n > 0) name = name.Substring(n, name.Length - n);
            dlg.Title = "Send " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return "";
        }
    }
}